package cn.iocoder.yudao.module.picturebook.service.membership;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.picturebook.controller.admin.classmember.vo.ClassMemberSaveReqVO;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.classes.ClassesDO;
import cn.iocoder.yudao.module.picturebook.service.classes.ClassesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.membership.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.membership.MembershipDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.picturebook.dal.mysql.membership.MembershipMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.picturebook.enums.ErrorCodeConstants.*;

/**
 * 会员信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class MembershipServiceImpl implements MembershipService {

    @Resource
    private MembershipMapper membershipMapper;

    @Override
    public Long createMembership(MembershipSaveReqVO createReqVO) {
        // 插入
        MembershipDO membership = BeanUtils.toBean(createReqVO, MembershipDO.class);
        membershipMapper.insert(membership);

        // 返回
        return membership.getId();
    }

    @Override
    public void updateMembership(MembershipSaveReqVO updateReqVO) {
        // 校验存在
        validateMembershipExists(updateReqVO.getId());
        // 更新
        MembershipDO updateObj = BeanUtils.toBean(updateReqVO, MembershipDO.class);
        membershipMapper.updateById(updateObj);
    }

    @Override
    public void deleteMembership(Long id) {
        // 校验存在
        validateMembershipExists(id);
        // 删除
        membershipMapper.deleteById(id);
    }

    @Override
    public void deleteMembershipListByIds(List<Long> ids) {
        // 删除
        membershipMapper.deleteByIds(ids);
    }


    private void validateMembershipExists(Long id) {
        if (membershipMapper.selectById(id) == null) {
            throw exception(MEMBERSHIP_NOT_EXISTS);
        }
    }

    @Override
    public MembershipDO getMembership(Long id) {
        return membershipMapper.selectById(id);
    }

    @Override
    public PageResult<MembershipDO> getMembershipPage(MembershipPageReqVO pageReqVO) {
        return membershipMapper.selectPage(pageReqVO);
    }


}